/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */

// reference local blank image
Ext.BLANK_IMAGE_URL = CONTEXT_PATH + '/ext/resources/images/default/s.gif';
Ext.SSL_SECURE_URL = CONTEXT_PATH + '/blank.html';

// Encoding for ajax form POST
/** @namespace Ext.lib */
Ext.lib.Ajax.defaultPostHeader += '; charset=UTF-8';

// The timeout
Ext.Ajax.timeout = 120000;

// Define main namespace
Ext.namespace("Main");

/**
 * Login entry point.
 */
Main.app = function() {

	return {
		// The main window
		mainWindow: null,

		// Main toolbar
		mainToolbar: null,

		// Main content
		mainContent: null,

		// The user
		user: {},

		// The status bar user information
		userData: null,

		// The status bar organization information
		organizationData: null,

		// The status bar
		statusBar: null,

		// The wait dialog
		waitDlg: null,

		/**
		 * Init method.
		 */
		init: function() {
			// Init quick-tips
			Ext.QuickTips.init();

			// Main toolbar
			this.mainToolbar = new Ext.Toolbar({region: 'north', height: 25});

			// Main content
			this.mainContent = new Ext.TabPanel({
				region: 'center',
				margins: '0 5 5 0',
				//resizeTabs: true,
				//minTabWidth: 135,
				enableTabScroll: true,
				layoutOnTabChange: true,
				deferredRender: true,
				activeTab: 0,
				items: [{
					xtype: 'panel',
					title: MSG['home'],
					closable: false,
					autoScroll: true,
					autoLoad: { url: CONTEXT_PATH + '/secure/home.do' }
				}]
			});

			this.userData = new Ext.Toolbar.TextItem(MSG['user.login']);

			this.organizationData = new Ext.Toolbar.TextItem(MSG['user.organization']);

			// The status bar
			this.statusBar = new Ext.StatusBar({
				items: [this.userData, ' ', this.organizationData]
			});

			// Main window implementation
			this.mainWindow = new Ext.Viewport({
				layout:'border',
				frame: true,
				items:[{
					title: MSG['title.app'],
					xtype: 'panel',
					iconCls: 'detecta-logo',
					region: 'center',
					layout: 'border',
					items: [this.mainToolbar, this.mainContent],
					bbar: this.statusBar
				}]
			});

			// Initialize modules
			this.initModules();

			// Show window
			this.mainWindow.on("show", this.onFirstShow, this);
			this.mainWindow.show();

			this.mainWindow.doLayout();

		},

		onFirstShow: function() {
			Ext.fly(this.userData.getEl().parentNode).addClass('x-status-text-panel').createChild({cls:'spacer'});
			Ext.fly(this.organizationData.getEl().parentNode).addClass('x-status-text-panel').createChild({cls:'spacer'});

			this.setUser(USER);
		},


		setUser: function(theUser) {
			this.user = Ext.apply(this.user, theUser);

			Ext.fly(this.userData.getEl()).update('<b>' + MSG['user.login'] + '</b>: ' + this.user.fullname);
			Ext.fly(this.organizationData.getEl()).update('<b>' + MSG['user.organization'] + '</b>: ' + this.user.organization.name);
		},

		getUser: function() {
			return this.user;
		},

		addPanel: function(panel) {
			var foundTabs = this.mainContent.find('title', panel.title);
			if(foundTabs.length > 0) {
				this.mainContent.activate(foundTabs[0]);
				return;
			}

			// Tab panel
			this.mainContent.add(panel).show();
			//this.mainContent.setActiveTab(panel);
		},

		getPanel: function(title) {
			var foundTabs = this.mainContent.find('title', title);
			if(foundTabs.length > 0) {
				return foundTabs[0];
			}	
			return null;
		},

		closePanel: function(panel) {
			this.mainContent.remove(panel);
		},

		initModules: function() {
			// Sort modules by key
			var module;
			var theModules = new Array();
			for (module in MODULES) {
				theModules.push(module);
			}
			theModules.sort();

			// Initialize modules
			for (var i = 0; i < theModules.length; ++i) {
				MODULES[theModules[i]].init();
			}
		}
	};
}();


/**
 * Before an ajax request... show wait dialog
 */
Ext.Ajax.on("beforerequest", function() {
	this.waitDlg = Ext.Msg.wait(MSG['wait.title'], MSG['wait.text']);
}, Main.app);

/**
 * After an ajax request... close wait dialog
 */
Ext.Ajax.on("requestcomplete", function() {
	if(this.waitDlg)  {
		this.waitDlg.hide();
	}
}, Main.app);

/**
 * After an ajax request... close wait dialog
 */
Ext.Ajax.on("requestexception", function() {
	if(this.waitDlg)  {
		this.waitDlg.hide();
		notifyError(MSG['error'], MSG['error.message']);
	}
}, Main.app);

